package com.company.project.command.inventory;

import com.company.project.command.PagingCommand;
import com.company.project.exception.NormalException;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;

import javax.validation.constraints.NotNull;
import java.util.Date;

@Data
@EqualsAndHashCode(callSuper = false)
public class InventoryCreateOrUpdateCommand {

    @NotNull(message = "id不能为空")
    private String id;

    /**
     * 所属仓库
     */
    @NotNull(message = "所属仓库不能为空")
    private String warehouseId;

    /**
     * 所属区域
     */
    @NotNull(message = "所属区域不能为空")
    private String region;

    /**
     * 所屬中心
     */

    @NotNull(message = "所属中心不能为空")
    private String owner;

    /**
     * 库位号
     */
    @NotNull(message = "库位号不能为空")
    private String inventoryLocationNo;

    /**
     * 操作者
     */
    private String createdBy;

    @CreatedDate
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date createdDate;

    @LastModifiedDate
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date lastModifiedDate;


    public void validData(BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            StringBuilder stringBuilder = new StringBuilder();
            for (ObjectError oe : bindingResult.getAllErrors()) {
                stringBuilder.append(oe.getDefaultMessage() + "\n");
            }
            throw new NormalException(stringBuilder.toString());
        }
    }

}
